package com.delivery.deliverysystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.delivery.deliverysystem.common.ServerResponse;
import com.delivery.deliverysystem.pojo.DO.ProductCategory;
import com.delivery.deliverysystem.pojo.DO.User;
import com.delivery.deliverysystem.pojo.VO.UserVO;
import com.delivery.deliverysystem.service.ProductCategoryService;
import com.delivery.deliverysystem.dao.ProductCategoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【product_category(产品类别表)】的数据库操作Service实现
* @createDate 2025-01-13 15:34:54
*/
@Service
@Slf4j
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory>
    implements ProductCategoryService{
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public boolean createProductCategory(ProductCategory category) {
        if (category == null)return false;
        QueryWrapper<ProductCategory> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("category_name",category.getCategoryName());
        ProductCategory productCategory = productCategoryMapper.selectOne(categoryQueryWrapper);
        if(productCategory != null) throw new RuntimeException("该类别已存在");
        int result = productCategoryMapper.insert(category);
        return result > 0;
    }

    @Override
    public boolean updateProductCategory(ProductCategory category) {
        Integer categoryId = category.getCategoryId();
        ProductCategory productCategory = productCategoryMapper.selectById(categoryId);
        if(productCategory == null) throw new RuntimeException("该类别不存在");
        UpdateWrapper<ProductCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("category_id", categoryId);

        if (category.getCategoryName() != null) {
            updateWrapper.set("category_name", category.getCategoryName());
        }
        if (category.getRemark() != null) {
            updateWrapper.set("remark", category.getRemark());
        }

        return productCategoryMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public List<ProductCategory> getCategoryList() {
        List<ProductCategory> categories = productCategoryMapper.selectList(null);
        if(categories!=null && !categories.isEmpty()){
            return categories.stream()
                    .map(category -> ProductCategory.builder()
                            .categoryId(category.getCategoryId())
                            .categoryName(category.getCategoryName())
                            .remark(category.getRemark())
                            .build())
                    .collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public ProductCategory getCategoryById(Integer id) {
        if(id == null)return null;
        ProductCategory category = productCategoryMapper.selectById(id);
        if(category != null){
            return ProductCategory.builder()
                    .categoryId(category.getCategoryId())
                    .categoryName(category.getCategoryName())
                    .remark(category.getRemark())
                    .build();
        }
        return null;
    }
}




